package com.zhongkai.web.control.declare;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.config.User;
import com.zhongkai.model.ticket.TPzJc;
import com.zhongkai.service.declare.DeclareService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.DateTool;

@Controller
public class DeclareControl {

	DeclareService declareService;

	@Resource
	public void setDeclareService(DeclareService declareService) {
		this.declareService = declareService;
	}
	
	@RequestMapping(value="/system/declare/declare_check.do")
	public String check(HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		String no=request.getParameter("ccdjh");		//车船登记号
		if(no==null || !no.matches("^[0-9]*[1-9][0-9]*$")){		//判断车船登记号是否非法
			out.write(CheckWord.showMsg("非法参数！", "/system/search/unisearch.jsp?module=declare/declare_check.do",request));
			return null;
		}
		Integer ccdjh=Integer.valueOf(no);		//生成整型车船登记号
		TDjCcdjxx ccdjxx=(TDjCcdjxx) declareService.selectById(TDjCcdjxx.class, ccdjh);		//根据车船登记号查找该车船登记信息
		if(ccdjxx==null){
			out.write(CheckWord.showMsg("找不到该车船登记号的信息！", "/system/search/unisearch.jsp?module=declare/declare_check.do",request));
			return null;
		}
		if(ccdjxx.getDjztDm().equals("02")){	//判断车船是否已迁出
			out.write(CheckWord.showMsg("当前车船登记状态为[已迁出]，无法进行车船申报！", "/system/search/unisearch.jsp?module=declare/declare_check.do",request));
			return null;
		}
		if(ccdjxx.getDjztDm().equals("09")){	//判断车船是否已注销
			out.write(CheckWord.showMsg("当前车船登记状态为[已注销]，无法进行车船申报！", "/system/search/unisearch.jsp?module=declare/declare_check.do",request));
			return null;
		}
		Date now=new Date();
		int currentYear2Int=Integer.parseInt(DateTool.formatDate(now, "yyyy"));		//当前年份
		int	sbqsn=Integer.parseInt(ccdjxx.getSbqsny().substring(0,4));		//申报起始年份
		if(currentYear2Int<sbqsn){
			out.write(CheckWord.showMsg("["+currentYear2Int+"]年的税额已缴，无需申报！", "/system/search/unisearch.jsp?module=declare/declare_check.do",request));
			return null;
		}
		request.setAttribute("checked", true);
		return "declare_form.jsp";
	}
	
	@RequestMapping(value="/system/declare/declare_calculate.do")
	public String calculate(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out=response.getWriter();
		String no=request.getParameter("ccdjh");		//车船登记号
		try {
			Integer ccdjh=Integer.valueOf(no);		//生成整型车船登记号
			TDjCcdjxx ccdjxx=(TDjCcdjxx) declareService.selectById(TDjCcdjxx.class, ccdjh);		//根据车船登记号查找该车船登记信息
			
			Date ssqzRq=null;
			String date=request.getParameter("ssqzRq");
			if(date==null){
				Date now=new Date();
				int currentYear2Int=Integer.parseInt(DateTool.formatDate(now, "yyyy"));		//当前年份
				ssqzRq=DateTool.parse(currentYear2Int+"-12-31", "yyyy-MM-dd");
			}
			else{
				ssqzRq=DateTool.setMonthToLastDay(DateTool.parse(date, "yyyy-MM"));
			}
			List declarelist=declareService.getDeclareList(ccdjxx, ssqzRq);
			request.setAttribute("declarelist", declarelist);
			return "declare_calculate.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			 String path=request.getContextPath();
			out.write("<script type='text/javascript' language='javascript'>alert('请先完善车船信息后再进行车船申报!');parent.location.href='"+path+"/system/book/car_register_update_form.jsp?ccdjh="+no+"';</script>");
		}
		return null;
	}
	
	@RequestMapping(value="/system/declare/declare_ticket.do")
	public String ticket(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String no=request.getParameter("ccdjh");		//车船登记号
		Integer ccdjh=Integer.valueOf(no);		//生成整型车船登记号
		TDjCcdjxx ccdjxx=(TDjCcdjxx) declareService.selectById(TDjCcdjxx.class, ccdjh);		//根据车船登记号查找该车船登记信息
		
		Date ssqzRq=null;
		String date=request.getParameter("ssqzRq");
		if(date==null){
			Date now=new Date();
			int currentYear2Int=Integer.parseInt(DateTool.formatDate(now, "yyyy"));		//当前年份
			ssqzRq=DateTool.parse(currentYear2Int+"-12-31", "yyyy-MM-dd");
		}
		else{
			ssqzRq=DateTool.setMonthToLastDay(DateTool.parse(date, "yyyy-MM"));
		}
		List declarelist=declareService.getDeclareList(ccdjxx, ssqzRq);
		request.setAttribute("ccdjh", ccdjh);
		request.setAttribute("ssqzRq", DateTool.formatDate(ssqzRq, "yyyy-MM"));
		request.setAttribute("declarelist", declarelist);
		return "declare_ticket.jsp";
	}
	
	//成功打印税票
	@RequestMapping(value="/system/declare/declare_print.do")
	public void print(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			String no=request.getParameter("ccdjh");		//车船登记号
			Integer ccdjh=Integer.valueOf(no);		//生成整型车船登记号
			TDjCcdjxx ccdjxx=(TDjCcdjxx) declareService.selectById(TDjCcdjxx.class, ccdjh);		//根据车船登记号查找该车船登记信息
			Date ssqzRq=DateTool.setMonthToLastDay(DateTool.parse(request.getParameter("ssqzRq"), "yyyy-MM"));
			User user=(User)request.getSession().getAttribute("user");
			Integer jcXh=Integer.valueOf(request.getParameter("jcXh"));	//结存序号
			String sphm=request.getParameter("sphm");	//税票号码
			String result=request.getParameter("result");	//打印结果，1-打印成功、0-打印失败
			List declarelist=declareService.getDeclareList(ccdjxx, ssqzRq);		//申报信息
			declareService.save(declarelist, user, ccdjxx, jcXh, ssqzRq);	//保存
			/**验证是否还有打印条目**/
			List continuedeclarelist=declareService.getDeclareList(ccdjxx, ssqzRq);		//申报信息
			if(continuedeclarelist!=null && continuedeclarelist.size()>0)
				out.print(2);
			else
				out.print(1);
		} catch (Exception e) {
			out.print(0);
			e.printStackTrace();
		}
	}
	
	//作废税票
	@RequestMapping(value="/system/declare/declare_invalidateTicket.do")
	public void invalidateTicket(HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		User user=(User)request.getSession().getAttribute("user");
		Integer jcXh=Integer.valueOf(request.getParameter("jcXh"));	//结存序号
		declareService.invalidateTicket(jcXh, user);	
	}
	
	//取票
	@RequestMapping(value="/system/declare/declare_takeTicket.do")
	public void takeTicket(HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		User user=(User)request.getSession().getAttribute("user");
		TPzJc pzJc=declareService.getPzJc(user);
		if(pzJc==null)
			out.print("empty");
		else
			out.print(pzJc.getJcXh()+","+pzJc.getSphmQ());
	}
}
